﻿using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using ShanNiang.AutoUpdate.Service;
using ShanNiang.AutoUpdate.Service.Implementations;
using ShanNiang.AutoUpdate.Service.Interfaces;
using ShanNiang.AutoUpdate.Service.Responses;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using System.Windows;

namespace ShanNiang.AutoUpdate
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        //private readonly ICheckVersionService _checkVersionService;
        //private readonly IUpdateService _updateService;
        //private readonly AutoUpdateConfig _autoUpdateConfig;
        //private readonly ILogger<MainWindow> _logger;
        //private PublishedClientResponse? _remoteClientResponse;

        public MainWindow(MainWindowViewModel mainWindowViewModel)
        {
            InitializeComponent();
            DataContext = mainWindowViewModel;
        }
        ///// <summary>
        ///// 初始化时执行
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private async void Window_Initialized(object sender, EventArgs e)
        //{
        //    var localVersion = _checkVersionService.GetLocalVersion();
        //    _remoteClientResponse = await _checkVersionService.GetRemoteVersionAsync();
        //    _logger.LogDebug($"Local:{localVersion};Remote{_remoteClientResponse?.RemoteVersion};");
        //    //如果本地版本足够新
        //    if (_remoteClientResponse != null && _remoteClientResponse.RemoteVersion.CompareTo(localVersion) <= 0)
        //    {
        //        _logger.LogDebug("");
        //        Environment.Exit(0);
        //    }
        //    else
        //    {
        //        Process.GetProcessesByName(_autoUpdateConfig.MainProcessName).ToList().ForEach(p => p.Kill());
        //        Loaded += Window_Loaded;
        //        //Process.Start(new ProcessStartInfo("pwsh") { UseShellExecute = true });
        //    }
        //}

        //private async void Window_Loaded(object sender, RoutedEventArgs e)
        //{
        //    //await Task.Delay(200);  //先让界面显示
        //    progUpdate.IsIndeterminate = true;
        //    await DoUpdate();
        //    progUpdate.IsIndeterminate = false;
        //    //MessageBox.Show("升级成功");
        //    //var mainProcess = $"{_autoUpdateConfig.MainProcessName}.exe";
        //    //if (File.Exists(Path.Combine(Environment.CurrentDirectory, mainProcess)))
        //    //{
        //    //    Process.Start(new ProcessStartInfo(mainProcess) { UseShellExecute = false });
        //    //}
        //    Environment.Exit(0);
        //}

        //private async Task DoUpdate()
        //{
        //    if (_remoteClientResponse != null && _remoteClientResponse.Url != null)
        //    {
        //        var packagePath = await _updateService.DownloadPackageAsync(_remoteClientResponse.Url, _remoteClientResponse.RemoteVersion);
        //        _checkVersionService.SetLocalVersion(_remoteClientResponse.RemoteVersion.ToString());
        //        _updateService.DoUpdate(packagePath);
        //    }
        //}

        //private void Window_Closed(object sender, EventArgs e)
        //{

        //}
    }
}